Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / jucer_BinaryResources.cpp
blobbc77821afd56c546efd4184eacd102ba4224941e
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_JucerDocument.h"
30 //==============================================================================
31 BinaryResources::BinaryResources()
35 BinaryResources::~BinaryResources()
39 BinaryResources& BinaryResources::operator= (const BinaryResources& other)
41 for (int i = 0; i < other.resources.size(); ++i)
42 add (other.resources[i]->name,
43 other.resources[i]->originalFilename,
44 other.resources[i]->data);
46 return *this;
49 void BinaryResources::changed()
51 if (document != 0)
53 document->changed();
54 document->refreshAllPropertyComps();
58 //==============================================================================
59 void BinaryResources::clear()
61 if (resources.size() > 0)
63 resources.clear();
64 changed();
68 const StringArray BinaryResources::getResourceNames() const
70 StringArray s;
72 for (int i = 0; i < resources.size(); ++i)
73 s.add (resources.getUnchecked(i)->name);
75 return s;
78 BinaryResources::BinaryResource* BinaryResources::findResource (const String& name) const throw()
80 for (int i = resources.size(); --i >= 0;)
81 if (resources.getUnchecked(i)->name == name)
82 return resources.getUnchecked(i);
84 return 0;
87 const BinaryResources::BinaryResource* BinaryResources::getResource (const String& name) const
89 return findResource (name);
92 const BinaryResources::BinaryResource* BinaryResources::getResourceForFile (const File& file) const
94 for (int i = resources.size(); --i >= 0;)
95 if (resources.getUnchecked(i)->originalFilename == file.getFullPathName())
96 return resources.getUnchecked(i);
98 return 0;
101 bool BinaryResources::add (const String& name, const File& file)
103 MemoryBlock mb;
105 if (! file.loadFileAsData (mb))
106 return false;
108 add (name, file.getFullPathName(), mb);
109 return true;
112 void BinaryResources::add (const String& name, const String& originalFileName, const MemoryBlock& data)
114 BinaryResource* r = findResource (name);
116 if (r == 0)
118 resources.add (r = new BinaryResource());
119 r->name = name;
122 r->originalFilename = originalFileName;
123 r->data = data;
124 deleteAndZero (r->drawable);
126 changed();
129 bool BinaryResources::reload (const int index)
131 return resources[index] != 0
132 && add (resources [index]->name,
133 File (resources [index]->originalFilename));
136 const String BinaryResources::browseForResource (const String& title,
137 const String& wildcard,
138 const File& fileToStartFrom,
139 const String& resourceToReplace)
141 FileChooser fc (title, fileToStartFrom, wildcard);
143 if (fc.browseForFileToOpen())
145 String name (resourceToReplace);
147 if (name.isEmpty())
148 name = findUniqueName (fc.getResult().getFileName());
150 if (! add (name, fc.getResult()))
152 AlertWindow::showMessageBox (AlertWindow::WarningIcon,
153 TRANS("Adding Resource"),
154 TRANS("Failed to load the file!"));
156 name = String::empty;
159 return name;
162 return String::empty;
165 const String BinaryResources::findUniqueName (const String& rootName) const
167 String nameRoot (makeValidCppIdentifier (rootName, true, true, false));
168 String name (nameRoot);
170 const StringArray resources (getResourceNames());
172 int suffix = 1;
174 while (resources.contains (name))
175 name = nameRoot + String (++suffix);
177 return name;
180 void BinaryResources::remove (const int i)
182 if (resources[i] != 0)
184 resources.remove (i);
185 changed();
189 const Drawable* BinaryResources::getDrawable (const String& name) const
191 BinaryResources::BinaryResource* const res = const_cast <BinaryResources::BinaryResource*> (getResource (name));
193 if (res == 0)
194 return 0;
196 if (res->drawable == 0 && res->data.getSize() > 0)
197 res->drawable = Drawable::createFromImageData (res->data.getData(), res->data.getSize());
199 return res->drawable;
202 const Image BinaryResources::getImageFromCache (const String& name) const
204 const BinaryResources::BinaryResource* const res = getResource (name);
206 if (res != 0 && res->data.getSize() > 0)
207 return ImageCache::getFromMemory (res->data.getData(), (int) res->data.getSize());
209 return Image();
212 void BinaryResources::loadFromCpp (const File& cppFileLocation, const String& cppFile)
214 StringArray cpp;
215 cpp.addLines (cppFile);
217 clear();
219 for (int i = 0; i < cpp.size(); ++i)
221 if (cpp[i].contains ("JUCER_RESOURCE:"))
223 StringArray tokens;
224 tokens.addTokens (cpp[i].fromFirstOccurrenceOf (":", false, false), ",", "\"'");
225 tokens.trim();
226 tokens.removeEmptyStrings();
228 const String resourceName (tokens[0]);
229 const int size = tokens[1].getIntValue();
230 const String originalFileName (cppFileLocation.getSiblingFile (tokens[2].unquoted()).getFullPathName());
232 jassert (resourceName.isNotEmpty() && size > 0);
234 if (resourceName.isNotEmpty() && size > 0)
236 const int firstLine = i;
238 while (i < cpp.size())
239 if (cpp [i++].contains ("}"))
240 break;
242 const String dataString (cpp.joinIntoString (" ", firstLine, i - firstLine)
243 .fromFirstOccurrenceOf ("{", false, false));
245 MemoryOutputStream out;
246 String::CharPointerType t (dataString.getCharPointer());
247 int n = 0;
249 while (! t.isEmpty())
251 const juce_wchar c = t.getAndAdvance();
253 if (c >= '0' && c <= '9')
254 n = n * 10 + (c - '0');
255 else if (c == ',')
257 out.writeByte ((char) n);
258 n = 0;
260 else if (c == '}')
261 break;
264 jassert (size < (int) out.getDataSize() && size > (int) out.getDataSize() - 2);
266 MemoryBlock mb (out.getData(), out.getDataSize());
267 mb.setSize (size);
269 add (resourceName, originalFileName, mb);
275 //==============================================================================
276 void BinaryResources::fillInGeneratedCode (GeneratedCode& code) const
278 if (resources.size() > 0)
280 code.publicMemberDeclarations << "// Binary resources:\n";
282 String defs;
283 defs << "//==============================================================================\n";
284 defs << "// Binary resources - be careful not to edit any of these sections!\n\n";
286 for (int i = 0; i < resources.size(); ++i)
288 code.publicMemberDeclarations
289 << "static const char* "
290 << resources[i]->name
291 << ";\nstatic const int "
292 << resources[i]->name
293 << "Size;\n";
295 const String name (resources[i]->name);
296 const MemoryBlock& mb = resources[i]->data;
298 defs << "// JUCER_RESOURCE: " << name << ", " << (int) mb.getSize()
299 << ", \""
300 << File (resources[i]->originalFilename)
301 .getRelativePathFrom (code.document->getFile())
302 .replaceCharacter ('\\', '/')
303 << "\"\n";
305 String line1;
306 line1 << "static const unsigned char resource_"
307 << code.className << "_" << name << "[] = { ";
309 defs += line1;
311 MemoryOutputStream out (65536);
312 int charsOnLine = line1.length();
314 for (size_t j = 0; j < mb.getSize(); ++j)
316 const int num = ((int) (unsigned char) mb[j]);
317 out << num << ',';
319 charsOnLine += 2;
320 if (num >= 10)
321 ++charsOnLine;
322 if (num >= 100)
323 ++charsOnLine;
325 if (charsOnLine >= 200)
327 charsOnLine = 0;
328 out << '\n';
332 out << (char) 0;
334 defs
335 << (const char*) out.getData()
336 << "0,0};\n\nconst char* "
337 << code.className << "::" << name
338 << " = (const char*) resource_" << code.className << "_" << name
339 << ";\nconst int "
340 << code.className << "::" << name << "Size = "
341 << (int) mb.getSize()
342 << ";\n\n";
345 code.staticMemberDefinitions += defs;
349 BinaryResources::BinaryResource::BinaryResource()
350 : drawable (0)
354 BinaryResources::BinaryResource::~BinaryResource()
356 deleteAndZero (drawable);